package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.mapper.AnswerService;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import cn.com.connext.scrm.web.rest.req.*;
import cn.com.connext.scrm.web.rest.resp.QueryAnswerByTypeResp;
import cn.com.connext.scrm.web.rest.resp.QueryAnswerDetailForManageResp;
import cn.com.connext.scrm.web.rest.resp.QueryAnswerDetailResp;
import cn.com.connext.scrm.web.rest.resp.QueryForManageResp;
import com.alibaba.fastjson.JSON;
import com.connext.common.exception.ServiceException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 问答相关接口
 */
@RestController
@RequestMapping("/api/answer")
public class AnswerResource {

    private final Logger log = LoggerFactory.getLogger(NoteResource.class);

    @Autowired
    private AnswerService answerService;

    /**
     * 创建问答
     */
    @PostMapping("/create")
    @ApiOperation(value = "创建问答", tags = {"问答列表"}, notes = "创建问答")
    public JsonResult create(@RequestBody AnswerCreateReq req) {
        log.info("创建问答，入参: {}", req);
        if (req == null) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return answerService.create(req);
    }
    /**
     * 创建问答
     */
    @PostMapping("/update")
    @ApiOperation(value = "创建问答", tags = {"问答列表"}, notes = "创建问答")
    public ResponseEntity<Boolean> update(@RequestBody AnswerUpdateReq req) {
        log.info("创建问答,入参req = {}", JSON.toJSONString(req));
        if (req == null) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return answerService.update(req);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除问答", tags = {"问答列表"}, notes = "删除问答")
    public ResponseEntity deleteNote(@RequestBody DeleteReq req) {
        log.info("删除问答,请求参数req=: {}", JSON.toJSONString(req));
        if (StringUtils.isEmpty(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return answerService.delete(req.getId());
    }

    @PostMapping("/audit")
    @ApiOperation(value = "审核问答", tags = {"问答列表"}, notes = "审核问答")
    public ResponseEntity audit(@RequestBody AnswerauditReq req) {
        log.info("审核问答,入参req = : {}", JSON.toJSONString(req));
        if (null == req) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return answerService.audit(req);
    }

    /**
     * 根据问答查询 问答 点赞数 评论数 浏览量
     */
    @PostMapping("/query")
    @ApiOperation(value = "根据小程序种类查询", tags = {"问答列表"}, notes = "根据问答种类查询")
    public ResponseEntity<QueryAnswerByTypeResp> queryByType(@RequestBody AnswerQueryReq req) {
        log.info("根据小程序种类查询问答，入参req = : {}", JSON.toJSONString(req));
        if (null == req) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return answerService.queryByType(req);
    }

    /**
     * 问答详情
     */
    @PostMapping("/detail")
    @ApiOperation(value = "问答详情", tags = {"问答列表"}, notes = "问答详情")
    public ResponseEntity<QueryAnswerDetailForManageResp> detail(@RequestBody DeleteReq req) {
        log.info("查询问答详情，入参req : {}", req);
        if (StringUtils.isBlank(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return answerService.detail(req.getId());
    }

    /**
     * 其他问答
     */
    @PostMapping("/other-answer")
    @ApiOperation(value = "其他问答查询", tags = {"问答列表"}, notes = "其他问答查询")
    public ResponseEntity<List<QueryAnswerDetailResp>> otherAnswer() {
        log.info("查询其他问答查询接口");
        return answerService.otherAnswer();
    }

    /**
     * 其他问答
     */
    @PostMapping("/hot-answer")
    @ApiOperation(value = "其他问答查询", tags = {"问答列表"}, notes = "其他问答查询")
    public ResponseEntity<List<QueryAnswerDetailResp>> hotAnswer(@RequestBody DeleteReq req) {
        log.info("查询其他问答查询接口");
        return answerService.hotAnswer(req);
    }

    @PostMapping("/manage-query")
    @ApiOperation(value = "后台管理问答查询", tags = {"问答列表"}, notes = "后台管理问答查询")
    public ResponseEntity<List<QueryForManageResp>> manageQuery(@RequestBody ManageQueryReq manageQueryReq) {
        log.info("后台管理问答查询");
        return answerService.manageQuery(manageQueryReq);
    }

    /**
     * 根据活动id查询帖子
     */
    @PostMapping("/query-by-actId")
    @ApiOperation(value = "根据活动id查询问答和帖子", tags = {"问答列表"}, notes = "其他问答查询")
    public ResponseEntity<Page> queryByActId(@RequestBody QueryByActIdReq req) {
        log.info("根据活动id查询问答和帖子 ，入参req: {}", JSON.toJSONString(req));
        if (StringUtils.isEmpty(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return answerService.queryByActId(req);
    }

    /**
     * 问答置顶或者取消
     */
    @PostMapping("/is-top")
    @ApiOperation(value = "问答置顶或者取消", tags = {"问答列表"}, notes = "其他问答查询")
    public ResponseEntity setIsTop(@RequestBody DeleteReq req) {
        log.info("问答置顶或者取消 : {}", req);
        if (StringUtils.isBlank(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        JsonResult result = new JsonResult();
        try {
            answerService.setIsTop(req.getId());
            result.setMessage("操作成功");
            result.setState(0);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("启用失败:{}", e);
            result.setState(1);
            result.setMessage("启用失败");
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 这是热门问答或者取消
     */
    @PostMapping("/isHot")
    @ApiOperation(value = "这是热门问答或者取消", tags = {"问答列表"}, notes = "其他问答查询")
    public ResponseEntity setIsHot(@RequestBody DeleteReq req) {
        log.info("这是热门问答或者取消 : {}", req);
        if (StringUtils.isBlank(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        JsonResult result = new JsonResult();
        try {
            answerService.setIsHot(req.getId());
            result.setMessage("操作成功");
            result.setState(0);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("启用失败:{}", e);
            result.setState(1);
            result.setMessage("启用失败");
            return ResponseEntity.ok(result);
        }
    }
}
